﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Model;
using BLL;
using NCRE学生考试端V1._0.选择题;
using NCRE学生考试端V1._0.悬浮框;
using System.Configuration;
using System.Data;


//using System.Runtime.InteropServices
//using System.ComponentModel
//using System.Data
//using System.Linq
//using System.Text
//using System.Data.SqlClient
//using System.Threading
//using Microsoft.Win32
//using System.Diagnostics





namespace NCRE学生考试端V1._0
{
    public partial class FrmMain : Form
    {
        public static Form frmmain;
        public FrmMain()
        {
            InitializeComponent();
            st = InitStudent(MyInfo.MystudentID(), MyInfo.MycollegeID());
            //secondColumnWidth = tableLayoutPanel1.ColumnStyles[1].Width
            frmmain = this;

        }
        //实例化一个学生实体
        public StudentInfoEntity studentinfo { get; set; }

        /// <summary>
        /// 加载学生信息
        /// </summary>


        public static StudentInfoEntity st = new StudentInfoEntity();

        private StudentInfoEntity InitStudent(string studentId, string collegeId)
        {
            st.studentID = studentId;
            st.CollegeID = collegeId;
            return st;
        }


        SelectQuestionBll sqBLL = new SelectQuestionBll();    //申明一个B层的实例
        List<SelectQuestionRecordEntity> listRecord;       //答题记录实体
        //记录考试倒计时的总时间

        private void FrmMain_Load(object sender, EventArgs e)
        {
            

            //显示悬浮框-李芬-2015年12月14日20:05:21
            if (frmxuanfukuang.frmxuanfuk == null)
            {
                frmxuanfukuang frmxfk = new frmxuanfukuang();

                frmxfk.Show();    //显示窗体
                frmxfk.Visible = false;
            }


            Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
            int height1 = ScreenArea.Height; //屏幕高度

            this.Left = 0;
            this.Top = 0;
            this.Height = height1;
            lblUnchoosed.BackColor = ColorTranslator.FromHtml("#99CCFF");
            lblChoosed.BackColor= ColorTranslator.FromHtml("#EDEDED");

            loadSelectQuestion();



            
            
        }


        #region 交卷的方法HandIn ---孟海滨
        /// <summary>
        /// 交卷的方法
        /// </summary>
        /// <returns>没有做的题目数量</returns>
        public int HandIn()
        {
            //1判断所有选择题是否做完
            try
            {
                listRecord = sqBLL.GetLstSelectQuestionRecordByStudentIdAndCollegeId(st, MyInfo.TimeTamp());
                int num = 0;
                for (int i = 0; i < listRecord.Count; i++)
                {
                    if (listRecord[i].ExamAnswer == string.Empty)
                    {
                        num++;
                    }
                }

                return num;
            }
            catch (Exception)
            {

                return 0;
            }

        }
        #endregion

        #region 没有这个button1按钮了
        //交卷
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    int num = HandIn();
        //    if (num > 0)
        //    {
        //        DialogResult dr = MessageBox.Show(this, "您还有" + num + "道题还没有做，是否要提交选择题。", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
        //        if (dr == DialogResult.OK)
        //        {

        //            tableLayoutPanel1.ColumnStyles[1].Width = 0;
        //            this.Width = 37 * Screen.PrimaryScreen.WorkingArea.Width / 100;
        //        }

        //    }
        //    else
        //    {
        //        DialogResult dr = MessageBox.Show(this, "是否要提交选择题", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
        //        if (dr == DialogResult.OK)
        //        {
        //            tableLayoutPanel1.ColumnStyles[1].Width = 0;
        //            this.Width = 37 * Screen.PrimaryScreen.WorkingArea.Width / 100;
        //        }
        //    }

        //    frmMain2 frmmain2 = new frmMain2();
        //    frmmain2.Show();
        //    this.Hide();
        //}
        #endregion


        #region 加载选择题loadSelectQuestion-卢彦聪-2018-4
        //使用Dictionary将所有选择题标签和选择题题干以键值对形式存起来
        public static Dictionary<Label, UCSelect> dict = new Dictionary<Label, UCSelect>();
        /// <summary>
        /// 加载选择题
        /// </summary>
        public Dictionary<Label, UCSelect> loadSelectQuestion()
        {
            try
            {

                #region 加载试题并切保存答题信息
                //1.查询数据，返回List<QuestionRecordEntity)
                listRecord = sqBLL.GetLstSelectQuestionRecordByStudentIdAndCollegeId(st, MyInfo.TimeTamp());
                if (listRecord.Count != 0)
                {
                    //2.将listQuestion添加到用户控件中
                    for (int i = 0; i < listRecord.Count; i++)
                    {
                        //添加第i个用户控件到窗体上
                        UCSelect uc = new UCSelect();
                        uc.Name = i.ToString();
                        uc.Top = 100;
                        uc.Left = 200;
                        uc.radioButton1.Click += radbtn_Click;
                        uc.radioButton2.Click += radbtn_Click;
                        uc.radioButton3.Click += radbtn_Click;
                        uc.radioButton4.Click += radbtn_Click;
                        //添加标签
                        uc.label2.Click += radbtn_Click;
                        uc.label3.Click += radbtn_Click;
                        uc.label4.Click += radbtn_Click;
                        uc.label5.Click += radbtn_Click;
                        flowLayoutPanel1.Controls.Add(uc);
                        //将试题信息绑定到用户控件上
                        uc.BindDataToSelf(listRecord[i], i);

                        //添加选择题标签：
                        Label label = new Label();
                        label.Name = (i + 1).ToString();
                        label.Text = (i + 1).ToString();
                        label.Width = 50;
                        label.Height = 50;
                        label.BorderStyle = BorderStyle.Fixed3D;
                        label.BackColor = ColorTranslator.FromHtml(" #99CCFF"); //未答题时标签颜色：蓝色系列
                        //label.BackColor = Color.Aquamarine;
                        label.TextAlign = ContentAlignment.MiddleCenter;
                        label.Font = new Font("宋书", 12, FontStyle.Bold);
                        label.FlatStyle = FlatStyle.Standard;
                        
                        label.Click += labelNum_Click;
                        //label.Click += new EventHandler(labelNum_Click);
                        label.MouseEnter += labelNum_MouseEnter;
                        flowLayPanel.Controls.Add(label);
                        dict.Add(label, uc);

                        //已答题的标签设置为灰色：
                        if (listRecord[i].ExamAnswer != "")
                        {
                            foreach (Label key in dict.Keys)
                            {
                                if (dict[key].Name.ToString().Equals(i.ToString()))
                                {
                                    //设置已答题的标签颜色：灰色系列
                                    key.BackColor = ColorTranslator.FromHtml("#EDEDED");
                                }
                            }
                        }
                    }
                    return dict;
                    //当前页数统计和显示

                }
                else
                {
                    MessageBox.Show(this, "您还没有试题，请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    return dict;
                }

                #endregion
            }
            catch (Exception)
            {
                System.Environment.Exit(0);
                throw;
            }
        }

        private void Label_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region 进入操作题
        private void btnNext_Click(object sender, EventArgs e)
        {
            #region 剩余题量
            int flagNum = HandIn();
            string strMsg = string.Empty;
            if (flagNum != 0)
            {
                strMsg = "您还有" + flagNum + "道题还没有做。" + "\r\n" + "“确定”进入操作题，“取消”进入剩余题作答";
            }
            #endregion

            if (flagNum != 0)
            {
                //提交进入操作题
                DialogResult dr = MessageBox.Show(this, strMsg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dr == DialogResult.OK)
                {
                    //确定按钮后，进入操作题界面
                    frmmain.Visible = false;
                    frmxuanfukuang.frmxuanfuk.Visible = true;
                    frmMain2 frm2 = new frmMain2();
                    frm2.Show();
                }
                else //跳到第一道未答题
                {
                    foreach (Label keys in dict.Keys)
                    {
                        if (keys.BackColor == ColorTranslator.FromHtml(" #99CCFF"))
                        {
                            int j = 0;
                            foreach (Label key in dict.Keys)
                            {
                                j = j + 1;
                                flowLayoutPanel1.Controls.SetChildIndex(dict[key], j);
                            }
                            flowLayoutPanel1.Controls.SetChildIndex(dict[keys], 0);
                            break;
                        }
                    }
                }
            }
            else
            {
                frmmain.Visible = false;
                frmxuanfukuang.frmxuanfuk.Visible = true;
                frmMain2 frm2 = new frmMain2();
                frm2.Show();
            }
        }
        #endregion


        #region 上一题按钮-卢彦聪-2018-4
        private void btnLeft_Click(object sender, EventArgs e)
        {
            UCSelect us = (UCSelect)flowLayoutPanel1.Controls[0];
            foreach (Label key in dict.Keys)
            {
                if (dict[key].Name == (int.Parse(us.Name) - 1).ToString())
                {
                    int j = 0;
                    foreach (Label keys in dict.Keys)
                    {
                        j = j + 1;
                        flowLayoutPanel1.Controls.SetChildIndex(dict[keys], j);
                    }
                    flowLayoutPanel1.Controls.SetChildIndex(dict[key], 0);
                }
                

                
            }

        }
        #endregion

        #region 下一题按钮-13期卢彦聪-2018-4
        public void btnRight_Click(object sender, EventArgs e)
        {
            UCSelect us = (UCSelect)flowLayoutPanel1.Controls[0];

            #region 如果是最后一道题，并且所有的选择题已经做完，点击“下一题”直接进入操作题作答：
            if ((int.Parse(us.Name) + 1).ToString() == "20")
            {
                #region 剩余题量
                int flagNum = HandIn();
                string strMsg = string.Empty;
                if (flagNum == 0)
                {
                    frmmain.Visible = false;
                    frmxuanfukuang.frmxuanfuk.Visible = true;
                    frmMain2 frm2 = new frmMain2();
                    frm2.Show();
                    //strMsg = "您的选择题已全部做完，是否进入操作题作答？" + "\r\n" + "“确定”进入操作题，“取消”检查选择题答案";
                }
                else //跳到第一道未答题
                {
                    foreach (Label keys in dict.Keys)
                    {
                        if (keys.BackColor == ColorTranslator.FromHtml(" #99CCFF"))
                        {
                            int j = 0;
                            foreach (Label key in dict.Keys)
                            {
                                j = j + 1;
                                flowLayoutPanel1.Controls.SetChildIndex(dict[key], j);
                            }
                            flowLayoutPanel1.Controls.SetChildIndex(dict[keys], 0);
                            break;
                        }
                    }
                }
                #endregion
            }
            #endregion

            foreach (Label key in dict.Keys)
            {
                if (dict[key].Name == (int.Parse(us.Name) + 1).ToString())
                {
                    int j = 0;
                    foreach (Label keys in dict.Keys)
                    {
                        j = j + 1;
                        flowLayoutPanel1.Controls.SetChildIndex(dict[keys], j);
                    }
                    //将下一道题显示在首页：
                    flowLayoutPanel1.Controls.SetChildIndex(dict[key], 0);
                }
            }
        }
        #endregion

        #region 实现单击左侧选择题序号时，右侧跳转到对应题-卢彦聪-2018-4
        private void labelNum_Click(object sender, EventArgs e)
        {
            //实现单击左侧选择题序号时，右侧跳转到对应题。
            Label lbl = (Label)(sender);

            int j = 0;
            foreach (Label keys in dict.Keys)
            {
                j = j + 1;
                flowLayoutPanel1.Controls.SetChildIndex(dict[keys], j);
            }
            flowLayoutPanel1.Controls.SetChildIndex(dict[lbl], 0);

        }
        #endregion

        #region 鼠标移到选择题标签上时变为小手的形状-卢彦聪-2018年4月29日08:34:12
        private void labelNum_MouseEnter(object sender, EventArgs e)
        {
            Label lbl = (Label)(sender);
            lbl.Cursor = Cursors.Hand;
        }
        #endregion


        #region 单击选择题选项后，自动跳到下一题-卢彦聪-2018-4
        private void radbtn_Click(object sender, EventArgs e)
        {
            UCSelect us = (UCSelect)flowLayoutPanel1.Controls[0];
            foreach (Label key in dict.Keys)
            {
                if (dict[key].Name == (int.Parse(us.Name) + 1).ToString())
                {
                    int j = 0;
                    foreach (Label keys in dict.Keys)
                    {
                        j = j + 1;
                        flowLayoutPanel1.Controls.SetChildIndex(dict[keys], j);

                    }
                    flowLayoutPanel1.Controls.SetChildIndex(dict[key], 0);
                }
            }
        }


        #endregion


    }
}